<?php

/**
 * 金果信息表
 */
use Common\Bridge;
use lib\core\AdminAction;

define('SITE_AUTH', 'goods-list');
require('../../../common.inc.php');

class goods extends AdminAction
{
    private $pdo_r;
    private $primary_key = 'Id';
    private $list_url = '/data/modules/gold/goods.php';

    public function doDefault()
    {
        $param['Id'] = (int)$_GET['Id'];

        $page = empty($_GET['page']) ? 1 : (int)$_GET['page'];
        $limit = 20;
        $offset = ($page - 1) * $limit;

        $where = "1";
        if ($param['Id'] > 0) {
            $where .= " AND Id = " . $param['Id'];
        }

        $primary_key = $this->primary_key;
        $count_sql = "SELECT COUNT(*) AS `num` FROM `Goods` WHERE $where";

        $count = $this->readDb($count_sql);
        $count = (int)$count[0]['num'];

        $sql = "SELECT * FROM `Goods` WHERE $where LIMIT $offset, $limit";
        $data = $this->readDb($sql);

        $catedata = \Center\Category::getAllCate();
        $cate = $catedata['data'];
        $smarty = $this->app->page();
        if ($count > 0) {
            if (!empty($data)) {
                $smarty->value('list', $data);
            }

            $page_info['page'] = ceil($count / $limit);
            $page_info['count'] = $count;
            $smarty->value('page_info', $page_info);

            $pagination = $this->pagination($count, $limit, $page);
            $smarty->value('pagination', $pagination);
        }

        $smarty->value('param', $param);
        $smarty->value('cate', $cate);
        $smarty->output('gold/goods.tpl');
    }

    /**
     * 利用SQL语句读取数据库并返回相应数据
     *
     * @param $sql
     * @return mixed
     */
    private function readDb($sql)
    {
        $pdo = $this->pdo_r;
        if (empty($pdo)) {
            $pdo = $this->pdo_r = Bridge::pdo(Bridge::DB_CENTER_W);
        }

        $obj = $pdo->query($sql);

        return $obj->fetchAll();
    }

    /**
     *
     */
    public function doEdit()
    {
        $id = (int)$_GET['Id'];
        if ($id < 1) {
            HDshowMsg('操作失败！', ['type' => 'error', 'desc' => 'Id为空']);
        }

        $sql = "SELECT * FROM Goods WHERE Id = $id";
        $list = $this->readDb($sql);

        $smarty = $this->app->page();
        $smarty->value('info', current($list));
        $smarty->output('gold/goodsedit.tpl');
    }

    public function doAdd()
    {
        HDshowMsg('添加失败！暂时不提供商品添加，如需添加请联系管理员！', ['type' => 'success', 'url' => $this->list_url]);
    }

    public function doUpdate()
    {
        $id = intval($_POST['Id']);
        if (empty($id) || empty($_POST['Title'])) {
            HDshowMsg('修改失败1！', ['type' => 'error', 'url' => $this->list_url]);
        }

        $data = [
            'AppleId' => trim($_POST['AppleId']),
            'Title' => trim($_POST['Title']),
            'Description' => trim($_POST['Description']),
            'Price' => (float)$_POST['Price'],
            'JCoin' => intval($_POST['JCoin']),
            'Donate' => intval($_POST['Donate']),
            'UpdateTime' => date('Y-m-d H:i:s'),
        ];
        $rel = \Center\Goods::update($id, $data);
        if ($rel) {
            HDshowMsg('修改成功！', ['type' => 'success', 'url' => $this->list_url]);
        } else {
            HDshowMsg('修改失败！', ['type' => 'error', 'url' => $this->list_url]);
        }
    }

    public function doDelete()
    {
        HDshowMsg('修改失败！暂时不提供商品删除，如需删除请联系管理员！', ['type' => 'success', 'url' => $this->list_url]);
    }

    public function doInput()
    {
        if (empty($_POST['AppleId'])) {
            HDshowMsg('添加失败！', ['type' => 'error', 'url' => $this->list_url]);
        }

        $data = [
            'AppleId' => trim($_POST['AppleId']),
            'Title' => trim($_POST['Title']),
            'Description' => trim($_POST['Description']),
            'Price' => trim($_POST['Price']),
            'JCoin' => intval($_POST['JCoin']),
            'Donate' => intval($_POST['Donate']),
            'UpdateTime' => date('Y-m-d H:i:s'),
        ];
        $id = Center\Goods::add($data);
        if ($id !== false && $id > 0) {
            HDshowMsg('修改成功！', ['type' => 'success', 'url' => $this->list_url]);
        } else {
            HDshowMsg('修改失败！', ['type' => 'error', 'url' => $this->list_url]);
        }
    }
}

$app->run();